class SettingsController < ApplicationController

    def index
        if authorize
            if session[:loginrole] == "Admin"
                @dashboard = "Go to Gradebook"
            end
            @username = session[:loginname]
    		user = User.find(:first, :conditions => [ "username = ?", @username])
    		person = Person.find(:first, :conditions => [ "user = ?", @username])
    		@firstname = person.firstname
    		@lastname = person.surname
            standing = ClassStanding.find(:first, :conditions => [ "id = ?", person.class_standing])
            contact = Contact.find(:first, :conditions => [ "id = ?", person.id])
            major = Major.find(:first, :conditions => [ "id = ?", person.major])
            
            @gender = person.gender
            @password = user.password
            @email = contact.email if not contact.email.nil?
            @age = person.age if not person.age.nil?
            @address = contact.address if not contact.address.nil?
            @city = contact.city if not contact.city.nil?
            @state = contact.state if not contact.state.nil?
            @zip = contact.zip if not contact.zip.nil?
            @phone = contact.phone if not contact.phone.nil?
            if(@gender == 'F')
           		@genders = "<option>Unknown</option><option>Male</option><option selected='selected'>Female</option>"
           	elsif(@gender == 'M')
           		@genders = "<option>Unknown</option><option selected='selected'>Male</option><option>Female</option>"
           	else
           	    @genders = "<option selected='selected'>Unknown</option><option>Male</option><option>Female</option>"
           	end
           	if standing.nil? 
           	    @standing = getClassStandings("")
           	else 
           	    @standing = getClassStandings(standing.standing)
           	end
           	if major.nil? 
           	    @major = getMajors("")
           	else 
           	    @major = getMajors(major.name)
           	end
           	
       		
        end
    end
	

    def update_settings
        flash[:notice] = ""
        @username = session[:loginname]
		user = User.find(:first, :conditions => [ "username = ?", @username])
		person = Person.find(:first, :conditions => [ "user = ?", @username])
        standing = ClassStanding.find(:first, :conditions => [ "id = ?", person.class_standing])    		
        contact = Contact.find(:first, :conditions => [ "id = ?", person.id])
        major = Major.find(:first, :conditions => [ "id = ?", person.major])


        if params[:age].to_f < 0.0
            flash[:notice] += "You haven't been born yet. Go back to heaven.<br />"
        else
            person.update_attribute("age", params[:age])
        end
        if params[:password] != ""
            user.update_attribute("password", params[:password])
        else
            flash[:notice] += "password can not be empty<br />"
        end
        if params[:email] == ""
            flash[:notice] += "email can not be empty<br />"
        elsif params[:email].index('@') == nil or params[:email].index('.') == nil
            flash[:notice] += "invalid email<br />"            
        else
            contact.update_attribute("email", params[:email])
        end

        contact.update_attribute("address", params[:address])
        contact.update_attribute("city", params[:city])
        contact.update_attribute("state", params[:state])
        contact.update_attribute("zip", params[:zip])
        contact.update_attribute("phone", params[:phone])
        
        if params[:gender] == "Male"
            person.update_attribute("gender", 'M')
        elsif params[:gender] == "Female"
            person.update_attribute("gender", 'F')
        elsif params[:gender] == "Unknown"
            person.update_attribute("gender", nil)
        end
        
        if params[:standing] != "-Select Class Standing-"
            person.update_attribute("class_standing", ClassStanding.find(:first, :conditions => [ "standing = ?", params[:standing]]).id)
        else
            person.update_attribute("class_standing", nil)
        end

        if params[:major] != "-Select Major-"
            person.update_attribute("major", Major.find(:first, :conditions => [ "name = ?", params[:major]]).id)
        else
            person.update_attribute("major", nil)
        end

        redirect_to :controller => :settings, :action => :index
    end

    def add_major
      if params[:newMajor] == ""
        flash[:notice] = "You must type a major"
      else
        Major.create(:name => params[:newMajor])
      end
      redirect_to :controller => :settings, :action => :index
    end


    def add_standing
      if params[:newStanding] == ""
        flash[:notice] = "You must type a class standing"
      else
        ClassStanding.create(:standing => params[:newStanding])
      end
      redirect_to :controller => :settings, :action => :index
    end

    def remove_major
      Major.delete(["name = ?" , Major.find(:first, :conditions => [ "name = ?", params[:major]]).id])
      redirect_to :controller => :settings, :action => :index
    end
    
    def remove_standing
      ClassStanding.delete(["standing = ?" , ClassStanding.find(:first, :conditions => [ "standing = ?", params[:standing]]).id])
      redirect_to :controller => :settings, :action => :index
    end

    def authorize
    	unless session[:loggedin] == true #&& session[:loginrole] == "admin" 
    		flash[:notice] = "Please log in" 
    		redirect_to :controller => :login, :action => :index 
        	return false
    	end 
    	return true
    end 
    

private

	def getClassStandings(curStanding)
	    if curStanding == ""
            value = "<option selected='selected'>-Select Class Standing-</option>"
		else
    		value = "<option>-Select Class Standing-</option>"
        end
		standings = ClassStanding.find(:all)
		standings.each{ |p|
            if p.standing == curStanding
                value += "<option selected='selected'>#{p[:standing]}</option>"
            else
                value += "<option>#{p[:standing]}</option>"
            end
		}
		value
	end
	
	def getMajors(curMajor)
	    if curMajor == ""
            value = "<option selected='selected'>-Select Major-</option>"
		else
    		value = "<option>-Select Major-</option>"
        end
		majors = Major.find(:all, :order => "name")
		majors.each{ |p|
            if p.name == curMajor
                value += "<option selected='selected'>#{p[:name]}</option>"
            else
                value += "<option>#{p[:name]}</option>"
            end
		}
		value
	end

end
